package com.rocksdata.mc.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.utils.DensityUtil;
import com.rocksdata.mc.Constant;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.TeacherInfo;


import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;

@ContentView(R.layout.activity_teacher_detail)
public class TeacherDetailActivity extends TitleBarActivity {
	@ViewInject(R.id.tv_user_name)
	private TextView tvUserName;
	@ViewInject(R.id.tv_mobile)
	private TextView tvMobile;
	@ViewInject(R.id.tv_email)
	private TextView tvEmail;
	@ViewInject(R.id.tv_department)
	private TextView tvDepartment;
	@ViewInject(R.id.tv_job_title)
	private TextView tvJobTitle;
	@ViewInject(R.id.tv_office)
	private TextView tvOffice;
	@ViewInject(R.id.tv_office_tel)
	private TextView tvOfficeTel;
    @ViewInject(R.id.layout_send_chat)
    private RelativeLayout layoutChat;
	@ViewInject(R.id.tv_relationship)
	private TextView tvRelationship;

    private TeacherInfo teacherInfo;
	private BusinessInterface.IUserInfoManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setTitleMiddleText("个人资料");
		String userId = getIntent().getStringExtra(Constant.KEY_USER_ID);
		manager = BusinessManager.getUserInfoManager();
		teacherInfo = manager.getTeacherInfoLocal(userId);
		setupView();
		getTeacherDetailRemote(userId);
	}

    @Event(value = { R.id.layout_send_chat, R.id.layout_relationship }, type = View.OnClickListener.class)
    private void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_send_chat:
//				Intent chatIntent = new Intent(TeacherDetailActivity.this, ChatActivity.class);
//				chatIntent.putExtra(Constant.KEY_USER_ID, teacherInfo.getStaffCode());
//				startActivity(chatIntent);
				break;
			case R.id.layout_relationship:
				if (teacherInfo.getClasses() != null && teacherInfo.getClasses().size() > 0) {
					Intent relationshipIntent = new Intent(TeacherDetailActivity.this, TeacherRelationshipActivity.class);
					relationshipIntent.putExtra("classes", (Serializable) teacherInfo.getClasses());
					startActivity(relationshipIntent);
				}
				break;
		}
	}
	
	private void getTeacherDetailRemote(String userId) {
		manager.getTeacherInfoRemote(userId, new BusinessInterface.OnBusinessResultListener() {
			@Override
			public void onBusinessResult(boolean isSuccess, Object object, String message) {
				if(isSuccess){
					teacherInfo = (TeacherInfo) object;
					setupView();
				}
			}
		});
	}

	private void setupView() {
//      如果是自己，发消息按钮不显示
		if(teacherInfo.getStaffCode().equals(UserInfoManager.getCurrentUserId())){
            layoutChat.setVisibility(View.GONE);
        }

		tvUserName.setText(teacherInfo.getStaffName());
		tvJobTitle.setText(teacherInfo.getTitle());
		tvDepartment.setText(teacherInfo.getDepartment().getDepartmentName());
		tvEmail.setText(teacherInfo.getEmail());
		tvOffice.setText(teacherInfo.getOffice());
        tvMobile.setText(teacherInfo.getMobile());
        tvOfficeTel.setText(teacherInfo.getTelephone());

		if(teacherInfo.getClasses() == null || teacherInfo.getClasses().size() == 0){
			tvRelationship.setText("无相关班级");
		}else{
			tvRelationship.setText(teacherInfo.getClasses().size() + "个班级");
			tvRelationship.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_arrow_right, 0);
			tvRelationship.setCompoundDrawablePadding(DensityUtil.dip2px(TeacherDetailActivity.this, 20));
		}

	}
}
